/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OldMethodDlg.java
 *
 * Created on 2010-9-16, 10:48:43
 */
package dataanalysis.gui.methoddlg;

import dataanalysis.main.MainView;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import weka.core.Capabilities;
import weka.core.CapabilitiesHandler;
import weka.core.Instances;
import weka.gui.explorer.ExplorerDefaults;

/**
 *
 * @author Administrator
 */
abstract public class BaseMethodDlg extends javax.swing.JDialog {

    protected final MainView m_manager;
    private JScrollPane m_treePanel;   //处理方法树形面板
    private Component m_editorComponent;  //处理方法所对应的参数
    private final JPanel m_paraContentPanel;   //参数面板
    protected MyObjectEditor m_objEditor = null;  //处理方法
    protected Object m_backup = null;   //之前所选的处理方法

    /** Creates new form BaseMethodDlg */
    public BaseMethodDlg(java.awt.Frame parent, boolean modal, final MainView manager) {
        super(parent, modal);
        m_manager = manager;
        initObjEditor();
        m_backup = m_objEditor.getValue();
        initComponents();

        m_paraContentPanel = new JPanel();
        m_methodContentPanel.setLayout(new BorderLayout());
        m_paraContentPanel.setLayout(new BorderLayout());
        m_paraScrollPanel.setViewportView(m_paraContentPanel);
        this.addEditor();

        this.setTitle("方法参数");
        this.addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                exit();
            }
        });

        this.setLocationRelativeTo(parent);
        m_paraPanel.setVisible(false);
        this.pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_methodPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        m_methodContentPanel = new javax.swing.JPanel();
        m_OKButton = new javax.swing.JButton();
        m_cancleButton = new javax.swing.JButton();
        m_paraPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        m_paraScrollPanel = new javax.swing.JScrollPane();
        m_paraButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        m_methodPanel.setPreferredSize(new java.awt.Dimension(280, 280));

        jLabel1.setText("选择方法:");

        javax.swing.GroupLayout m_methodContentPanelLayout = new javax.swing.GroupLayout(m_methodContentPanel);
        m_methodContentPanel.setLayout(m_methodContentPanelLayout);
        m_methodContentPanelLayout.setHorizontalGroup(
            m_methodContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 392, Short.MAX_VALUE)
        );
        m_methodContentPanelLayout.setVerticalGroup(
            m_methodContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 197, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout m_methodPanelLayout = new javax.swing.GroupLayout(m_methodPanel);
        m_methodPanel.setLayout(m_methodPanelLayout);
        m_methodPanelLayout.setHorizontalGroup(
            m_methodPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addComponent(m_methodContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        m_methodPanelLayout.setVerticalGroup(
            m_methodPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(m_methodPanelLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_methodContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        m_OKButton.setText("确  定");
        m_OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_OKButtonActionPerformed(evt);
            }
        });

        m_cancleButton.setText("取  消");
        m_cancleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_cancleButtonActionPerformed(evt);
            }
        });

        m_paraPanel.setPreferredSize(new java.awt.Dimension(280, 280));

        jLabel2.setText("设置参数:");

        m_paraScrollPanel.setPreferredSize(new java.awt.Dimension(326, 251));

        javax.swing.GroupLayout m_paraPanelLayout = new javax.swing.GroupLayout(m_paraPanel);
        m_paraPanel.setLayout(m_paraPanelLayout);
        m_paraPanelLayout.setHorizontalGroup(
            m_paraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(m_paraPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(328, Short.MAX_VALUE))
            .addComponent(m_paraScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
        );
        m_paraPanelLayout.setVerticalGroup(
            m_paraPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(m_paraPanelLayout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_paraScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                .addContainerGap())
        );

        m_paraButton.setText("参  数");
        m_paraButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_paraButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(m_paraPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                    .addComponent(m_methodPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(m_paraButton)
                    .addComponent(m_cancleButton)
                    .addComponent(m_OKButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(m_OKButton)
                        .addGap(18, 18, 18)
                        .addComponent(m_cancleButton)
                        .addGap(18, 18, 18)
                        .addComponent(m_paraButton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(m_methodPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(m_paraPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_cancleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_cancleButtonActionPerformed
        cancleAction();
    }//GEN-LAST:event_m_cancleButtonActionPerformed

    private void m_paraButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_paraButtonActionPerformed
        m_paraPanel.setVisible(!m_paraPanel.isVisible());
        this.pack();
    }//GEN-LAST:event_m_paraButtonActionPerformed

    private void m_OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_OKButtonActionPerformed
        OKAction();
    }//GEN-LAST:event_m_OKButtonActionPerformed
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                OldMethodDlg dialog = new OldMethodDlg(new javax.swing.JFrame(), true, null);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton m_OKButton;
    private javax.swing.JButton m_cancleButton;
    private javax.swing.JPanel m_methodContentPanel;
    private javax.swing.JPanel m_methodPanel;
    private javax.swing.JButton m_paraButton;
    private javax.swing.JPanel m_paraPanel;
    private javax.swing.JScrollPane m_paraScrollPanel;
    // End of variables declaration//GEN-END:variables

    private void exit() {
        this.setVisible(false);
        m_paraPanel.setVisible(false);
        this.pack();
    }

    protected void updateUIState() {
        if (m_objEditor == null) {
            return;
        }
        boolean bOKButton = true;
        boolean bParaButton = true;
        Capabilities currentFilter = m_objEditor.getCapabilitiesFilter();
        Object capHandler = m_objEditor.getValue();
        Capabilities currentSchemeCapabilities = null;
        if (capHandler != null && currentFilter != null
                && (capHandler instanceof CapabilitiesHandler)) {
            currentSchemeCapabilities = ((CapabilitiesHandler) capHandler).getCapabilities();

            if (!currentSchemeCapabilities.supportsMaybe(currentFilter)
                    && !currentSchemeCapabilities.supports(currentFilter)) {
                bOKButton = false;
                bParaButton = false;
                if (m_paraPanel.isVisible()) {
                    m_paraPanel.setVisible(false);
                    this.pack();
                }
            }
        }
        m_OKButton.setEnabled(bOKButton);
        m_paraButton.setEnabled(bParaButton);
    }

    public void updateCapabilitiesFilter(Instances data) {
        Instances tempInst;
        Capabilities filterClass;

        if (!ExplorerDefaults.getInitGenericObjectEditorFilter()) {
            tempInst = new Instances(data, 0);
        } else {
            tempInst = new Instances(data);
        }

        try {
            filterClass = Capabilities.forInstances(tempInst);
        } catch (Exception e) {
            filterClass = new Capabilities(null);
        }

        // set new filter
        m_objEditor.setCapabilitiesFilter(filterClass);
    }

    abstract protected void setEditorValueDoc();

    abstract protected void editorChangeAction();

    abstract protected void initObjEditor();

    protected void recover() {
        m_objEditor.setValue(m_backup);
        this.updateTreePanel();
    }

    protected void updateTreePanel() {
        m_treePanel = m_objEditor.getTreePanel();//m_treePanel并不绑定m_objEditor,而是从中产生的一个临时变量
        m_methodContentPanel.removeAll();
        m_methodContentPanel.add(m_treePanel);
        m_methodContentPanel.validate();//使刚添加的树形面板生效
    }

    protected void cancleAction() {
        this.recover();
        this.exit();
    }

    protected void OKAction() {
        m_backup = m_objEditor.getValue();
        this.setEditorValueDoc();
        this.exit();
    }

    public void RebuildEditor(){
        this.initObjEditor();
        m_objEditor.setValue(m_backup);
        this.addEditor();
    }

    private void addEditor() {
        m_methodContentPanel.removeAll();
        m_paraContentPanel.removeAll();
        m_treePanel = m_objEditor.getTreePanel();//m_treePanel并不绑定m_objEditor,而是从中产生的一个临时变量
        m_methodContentPanel.add(m_treePanel, BorderLayout.CENTER);//设置选择方法面板
        m_editorComponent = m_objEditor.getCustomEditor();
        m_paraContentPanel.add(m_editorComponent, BorderLayout.NORTH);//设置参数设置面板
        m_methodContentPanel.repaint();
        m_methodContentPanel.validate();
        m_paraContentPanel.repaint();
        m_paraContentPanel.validate();
    }
}
